<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021-04-27
 * Time: 22:51
 */
namespace app\admin\controller;
use think\facade\Db;
use think\Request;

class Login
{
    public function login(Request $request){
        if($request->isAjax()){
            $data = $request->param();
            $array = [
                'username|用户名'=>'require',
                'password|密码'=>'require',
                'captcha|验证码'=>'require|captcha',
            ];
            list($code,$msg) = Validate($data,$array);
//            return json_to_respos($msg);
            if(!$code){
                return json_to_respos($msg,201);
            }
            $adminuser = Db::name('system_admin')
                ->where('username',$data['username'])
                ->findOrEmpty();
            if(empty($adminuser)){
                return json_to_respos('用户不存在',201);
            }
            if(!password_decode($data['password'],$adminuser['password'])){
                return json_to_respos('账号或密码错误',201);
            }
            session('adminuser',$adminuser);
            return json_to_respos('登录成功',200);
        }
        return view();
    }
}